<?php

/**
 * 编写：祝踏岚
 * 用于对收款账号的操作
 */

namespace app\manage\model;

use think\Model;

class GoodsModel extends Model{
	//表名
	protected $table = 'ly_users_pledge_orders';
	/**
	 * 开关
	 */
	public function onOff(){
		if(!request()->isAjax()) return '非法提交';

		$param = input('post.');
		if(!$param) return '提交失败';

		//提取信息备用
		$bankInfo = $this->where('id',$param['id'])->find();

		$res = $this->where('id',$param['id'])->setField($param['field'],$param['val']);

		switch ($param['field']) {
			case 'status':
				$fieldName = '状态';
				break;
			
		}
		if($param['val']==1){
			$fieldState = '开启';
		}else{
			$fieldState = '关闭';
		}
		//添加操作日志
		model('Actionlog')->actionLog(session('manage_username'),'将质押碎片:'.$bankInfo['name'].'的'.$fieldName.$fieldState,1);

		return 1;
	}
	/**
	 * 删除
	 */
	public function del(){
		if(!request()->isAjax()) return '非法提交';

		$param = input('post.');
		if(!$param) return '提交失败';

		//提取信息备用
		$info = $this->where('id',$param['id'])->find();

        $delRes = $this->where('id',$param['id'])->update(['status'=>-1]);
        db('users')->where(['id'=>$info['uid']])->update(['is_auth'=>0]);
		if(!$delRes) return '删除失败';

		//添加操作日志
		model('Actionlog')->actionLog(session('manage_username'),'删除KYC',1);

		return 1;
	}
	/**
	 * 审核
	 */
	public function check(){
		$param = input('post.');
		if(!$param) return '提交失败';

		$controlAuditTime = cache('CA_controlAuditTime'.session('manage_userid')) ? cache('CA_controlAuditTime'.session('manage_userid')) : time()-2;
		if(time() - $controlAuditTime < 2){
			return ' 2 秒内不能重复提交';
		}
		cache('CA_controlAuditTime'.session('manage_userid'), time(), 10);

		$id = $param['id'];

		//获取订单信息
		$where[] = array('id','=',$id);
		$where[] = array('status','=',1);
		$info = $this->where($where)->find();
		if (!$info) return '信息不存在';
		$info = $info->toArray();
		// 更新订单
        $res = $this->allowField(true)->save($param,$id);
        if($param['status']==2){
            db('users')->where(['id'=>$info['uid']])->update(['is_auth'=>1]);
        }
		if(!$res) return '操作失败1';

		switch ($param['status']) {
			case 3:
				//添加操作日志
				model('Actionlog')->actionLog(session('manage_username'),'审核发布者KYC。处理状态：审核未通过',1);
				break;

			case 2:
				//添加操作日志
				model('Actionlog')->actionLog(session('manage_username'),'审核发布者KYC。处理状态：审核通过',1);
				break;
		}

		return 1;
	}
}